<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_ProductController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        parent::loadTemplate('alpona');
        $this->view->headTitle = "Bảng tồn kho";
	}
	
	public function indexAction(){
		$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
		$model = new Admin_Model_Product();
        $data = $model->getDatas();
        
        $this->view->data = $data;
	}
    
    public function createAction(){
        $scripts = array('autoNumeric.js');
		parent::addScripts($scripts);
        
    	$request = $this->_request;
        
        $brand_model = new Admin_Model_Brand();
        $brands = $brand_model->getDatas();
        
        $custom_model = new Admin_Model_Customer();
        $customers = $custom_model->getDatas();
               
        $this->view->brands = $brands;
        $this->view->customers = $customers;
        
        if($request->isPost()){
            $validator = new Admin_Form_ValidateProduct($request->getParams());
            
            if($validator->isError()){
                $this->view->isError = $validator->isError();
                $this->view->messageError = $validator->getMessageError();
                $this->view->classError = $validator->getClassError();
                $this->view->data = $validator->getData();
            }else{
                $model = new Admin_Model_Product();
                $rs = $model->saveData($validator->getData());
                
                //$this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product', 'action' => 'index'), null, true);
            }
        }
    }
    
    public function editAction(){
        $scripts = array('autoNumeric.js');
		parent::addScripts($scripts);
        
        $request = $this->_request;
        
        $brand_model = new Admin_Model_Brand();
        $brands = $brand_model->getDatas();
        
        $custom_model = new Admin_Model_Customer();
        $customers = $custom_model->getDatas();
               
        $this->view->brands = $brands;
        $this->view->customers = $customers;
        
        if($request->isPost()){
            $model = new Admin_Model_Product();
            $data = $model->getData($request->getParam('id'));
            
            $this->view->data = $data;
            
            $save = $request->getParam('save', false);
            
            if($save){
                $validator = new Admin_Form_ValidateProduct($request->getParams(), true, $data);
            
                if($validator->isError()){
                    $this->view->isError = $validator->isError();
                    $this->view->messageError = $validator->getMessageError();
                    $this->view->classError = $validator->getClassError();
                    $this->view->data = $validator->getData();
                }else{
                    $rs = $model->saveData($validator->getData());
                    
                    $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product', 'action' => 'index'), null, true);
                }
            }
        }
    }
    
    public function deleteAction(){
        $request = $this->_request;
        
        if($request->isPost()){
            $model = new Admin_Model_Product();
            $rs = $model->deleteData($request->getParam('id'));
        }
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product', 'action' => 'index'), null, true);
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function quantityAction(){
        $this->_helper->_viewRenderer->setNoRender();
    	$request = $this->_request;
        
        $model = new Admin_Model_Product();
        $validator = new Admin_Form_ValidateProductQuantity($request->getParams());
        $result = $model->updateQuantity($validator->getData());
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product', 'action' => 'index'), null, true);
    }
}